package com.mall.user.services;

import com.mall.user.IVerifyService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dal.entitys.Member;
import com.mall.user.dal.entitys.UserVerify;
import com.mall.user.dal.persistence.MemberMapper;
import com.mall.user.dal.persistence.UserVerifyMapper;
import com.mall.user.dto.UserVerifyRequest;
import com.mall.user.dto.UserVerifyResponse;
import com.mall.user.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

/**
 * @ClassName VerifyServiceImpl
 * @Description
 * @Author fan
 * @Date 2021/5/22 15:16
 **/

@Slf4j
@Component
@Service
public class VerifyServiceImpl implements IVerifyService {

    @Autowired
    MemberMapper memberMapper;
    @Autowired
    UserVerifyMapper userVerifyMapper;

    @Override
    public UserVerifyResponse verify(UserVerifyRequest request) {
        UserVerifyResponse userVerifyResponse = new UserVerifyResponse();
        try {
            request.requestCheck();
            Example example = new Example(Member.class);
            example.createCriteria().andEqualTo("username",request.getUserName());
            Member member = new Member();
            member.setIsVerified("Y");
            int i = memberMapper.updateByExampleSelective(member, example);
            Example example1 = new Example(UserVerify.class);
            example1.createCriteria().andEqualTo("uuid",request.getUuid());
            UserVerify userVerify = new UserVerify();
            userVerify.setIsVerify("Y");
            int j = userVerifyMapper.updateByExampleSelective(userVerify, example1);
            if (i==1 && j==1){
                userVerifyResponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
                userVerifyResponse.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
            }else{
                userVerifyResponse.setCode(SysRetCodeConstants.USER_INFOR_UPDATE_FAIL.getCode());
                userVerifyResponse.setMsg(SysRetCodeConstants.USER_INFOR_UPDATE_FAIL.getMessage());
            }
        } catch (Exception e) {
            log.error("VerifyServiceImpl.verify Occur Exception :"+e);
            ExceptionProcessorUtils.wrapperHandlerException(userVerifyResponse,e);
        }
        return userVerifyResponse;
    }


}
